/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kiosk;

import java.net.URL;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javax.xml.datatype.Duration;
import queues.QueuesClientWindowController;
import server.SubjectInfo;

/**
 * FXML Controller class
 *
 * @author Renato Rosa <2101076@my.ipleiria.pt>
 */
public class SubjectInfoWindowController extends QueuesClientWindowController<Kiosk> {

    private SubjectInfo info;
    private Thread clockThread;
    private boolean running = true;
    @FXML
    private Label labelWaitingTime;
    @FXML
    private Label labelCurrentTime;
    @FXML
    private Label labelTitle;
    @FXML
    private Label labelNumber;
    @FXML
    private Label labelCurrentTicket;
    @FXML
    private Label labelNextTicket;
    @FXML
    private Label labelInfo;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //nothing to do
    }

    public void setSubjectInfo(SubjectInfo infoSubject) {
        info = infoSubject;
        finishClockThread();
        Duration time = info.getWaitingTime();
        final String minutes = String.format("%d min", time.getHours() * 60 + time.getMinutes());

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                labelTitle.setText(info.getSubject().getMnemonic() + " - " + info.getSubject().getName());
                labelWaitingTime.setText(minutes);
                labelNumber.setText(Integer.toString(info.getCount()));
                labelCurrentTicket.setText(info.getCurrentTicketText());
                labelNextTicket.setText(info.getNextTicketText());
                labelInfo.setText("");
            }
        });
        
        running = true;
        clockThread = new ClockThread();
        clockThread.start();
    }

    @FXML
    private void requestTicket(ActionEvent ae) {
        finishClockThread();
        if (app != null) {
            app.requestTicket(info.getSubject().getId());
            app.presentKiosk();
        }
    }

    @FXML
    private void cancel(ActionEvent ae) {
        finishClockThread();
        if (app != null) {
            app.presentKiosk();
        }
    }
    
    private void finishClockThread() {
        running = false;
        if (clockThread != null) {
            try {
                clockThread.interrupt();
                clockThread.join(1000);
            } catch (InterruptedException ex) {
                //
            }
        }
    }

    private class ClockThread extends Thread {
        private int timeSec = 30;

        public ClockThread() {
            this.setName("Clock thread");
            setDaemon(true);
        }

        @Override
        public void run() {
            while (running) {
                final Calendar now = new GregorianCalendar();
                int hour = now.get(Calendar.HOUR_OF_DAY);
                int minute = now.get(Calendar.MINUTE);
                final String time = String.format("%02d:%02d", hour, minute);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        if (timeSec <= 0) {
                            cancel(null);
                        }
                        labelCurrentTime.setText(time);
                        labelInfo.setText("A fechar em " + Integer.toString(timeSec) + " segundos");
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    return;
                }
                timeSec--;
            }
        }
    }
}
